package org.jypj.dev.intercept;

import org.jypj.dev.dao.ShoesDao;
import org.jypj.dev.model.Shoes;
import org.jypj.dev.util.PropertyUtil;
import org.jypj.dev.vo.BaseInfo;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

public class LoginHandlerInterceptor extends HandlerInterceptorAdapter {

    @Resource
    private ShoesDao shoesDao;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        HttpSession session = request.getSession();
        BaseInfo baseInfo = (BaseInfo) session.getAttribute("baseInfo");
        if (baseInfo == null) {
            baseInfo = new BaseInfo();
            Shoes shoes = new Shoes();
            shoes.setStatus("1");
            List<Shoes> shoes1 = shoesDao.selectAllByShoes(shoes);
            for (Shoes shoes2 : shoes1) {
                baseInfo.getShoeStyles().add(shoes2.getStyle());
                baseInfo.getShoeColors().add(shoes2.getColor());
                baseInfo.getShoeMaterials().add(shoes2.getMaterial());
                baseInfo.getShoeBelows().add(shoes2.getShoeBelow());
            }
            baseInfo.setFileServerPath(PropertyUtil.getFileServerPath());
            session.setAttribute("baseInfo", baseInfo);
        }
        return true;
    }
}
